package com.chocus.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chocus.core.bean.product.Brand;
import com.chocus.core.service.product.BrandService;

import cn.itcast.common.page.Pagination;

@Controller
@RequestMapping("/brand/")
public class BrandController {

	@Autowired
	private BrandService brandService;

	/**
	 * 品牌列表
	 * 
	 * @return
	 */
	@RequestMapping("list.do")
	public String list(Integer pageNo, String name, Integer isDisplay, Model model) {
		Pagination pagination = brandService.getBrandByQuery(name, isDisplay, pageNo);
		model.addAttribute("pagination", pagination);
		model.addAttribute("name", name);
		model.addAttribute("isDisplay", isDisplay);
		return "brand/list";
	}

	/**
	 * 品牌列表
	 * 
	 * @return
	 */
	@RequestMapping("toEdit.do")
	public String toEdit(long id, Model model) {
		Brand brand = brandService.getBrandById(id);
		model.addAttribute("brand", brand);
		return "brand/edit";
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	@RequestMapping("edit.do")
	public String edit(Brand brand, Model model) {
		brandService.update(brand);
		return "redirect:list.do";
	}

	/**
	 * 批量删除
	 * 
	 * @return
	 */
	@RequestMapping("deletes.do")
	public String deletes(Long[] ids, Model model, Integer pageNo, String name, Integer isDisplay,
			RedirectAttributes ra) {
		int count = brandService.deletes(ids);
		ra.addAttribute("pageNo", pageNo);
		ra.addAttribute("name", name);
		ra.addAttribute("isDisplay", isDisplay);
		return "redirect:list.do";
	}
}
